package top.api.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import top.api.common.RedisKey;
import top.api.common.ResUlt;
import top.api.domain.dto.SysUserInfoLoginDto;
import top.api.domain.pojo.UserInfo;
import top.api.mapper.UserInfoMapper;
import top.api.service.loginUserInfoService;
import top.api.utils.BeanCopyUtils;
import top.api.utils.RedisCache;

@Service
@AllArgsConstructor
public class loginUserInfoServiceImpl implements loginUserInfoService {

    private final UserInfoMapper userInfoMapper;

    private final RedisCache redisCache;
    @Override
    public ResUlt<Object> login(SysUserInfoLoginDto sysUserInfoLoginDto) {
        // 查询验证码是否正确, 无论正确错误都把验证码删除
        if (LoginAndRegister(sysUserInfoLoginDto, redisCache)) return ResUlt.error("图片验证码错误!");

        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserInfo::getEmail, sysUserInfoLoginDto.getEmail());
//        lqw.eq(UserInfo::getNickName, sysUserInfoLoginDto.getNickname());
        lqw.eq(UserInfo::getPassword, sysUserInfoLoginDto.getPassword());
        UserInfo userInfo = userInfoMapper.selectOne(lqw);
        if (ObjectUtils.isEmpty(userInfo)) {
            return ResUlt.error("账号或密码错误!");
        }

//        // 查询是否被冻结
//        if (1 == userInfo.getStatus()) {
//            return ResUlt.error("账号异常!");
//        }

        // 记录ip
//        user.setIp(request.getRemoteAddr());
//        baseMapper.updateById(user);

        StpUtil.login(userInfo.getUserId(), true);

        String tokenValue = StpUtil.getTokenValue();
        String loginId = StpUtil.getLoginId().toString();
        System.out.println(loginId);
        return ResUlt.success("登录成功", tokenValue);

    }

    @Override
    public ResUlt<Object> register(SysUserInfoLoginDto sysUserInfoLoginDto) {
        int insert =0;
        // 查询验证码是否正确, 无论正确错误都把验证码删除
        if (LoginAndRegister(sysUserInfoLoginDto, redisCache)) return ResUlt.error("图片验证码错误!");
        UserInfo userInfo = BeanCopyUtils.copyBean(sysUserInfoLoginDto, UserInfo.class);
        LambdaQueryWrapper<UserInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserInfo::getEmail, sysUserInfoLoginDto.getEmail());
        UserInfo info = userInfoMapper.selectOne(lqw);

        if(ObjectUtils.isEmpty(info)){
            insert= userInfoMapper.insert(userInfo);
        }
        if (insert==0){
            return ResUlt.error("注册失败");
        }

        return ResUlt.success("注册成功");
    }


        /**
         * @Description: 登录和注册校验验证码方法
         * @Param: [sysUserInfoLoginDto, redisCache]
         * @return: boolean
         * @Author: zqh
         * @Date: 2024/5/28
         */
    static boolean LoginAndRegister(SysUserInfoLoginDto sysUserInfoLoginDto, RedisCache redisCache) {
        String picCode = redisCache.getCacheObject(RedisKey.getPicCodeKey() + ":" + sysUserInfoLoginDto.getUuid());
        if (!sysUserInfoLoginDto.getPicCode().equals(picCode)) {
            redisCache.deleteObject(RedisKey.getPicCodeKey() + ":" + sysUserInfoLoginDto.getUuid());
            return true;
        }
        redisCache.deleteObject(RedisKey.getPicCodeKey() + ":" + sysUserInfoLoginDto.getUuid());
        return false;
    }
}
